package com.doget.data.db;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

//@Database(entities = {UserInfo.class}, version = 1)
//public abstract class AppDatabases extends RoomDatabase {
//    public abstract UserInfoDao userDao();
//}

@Database(entities = {UserInfo.class}, version = 2, exportSchema = false)
public abstract class AppDatabases extends RoomDatabase {

    private static final String DB_NAME = "UserDatabase.db";
    private static volatile AppDatabases instance;

    static synchronized AppDatabases getInstance(Context context) {
        if (instance == null) {
            instance = create(context);
        }
        return instance;
    }

    private static AppDatabases create(final Context context) {
        return Room.databaseBuilder(
                context,
                AppDatabases.class,
                DB_NAME).
                addMigrations(MIGRATION_1_2).
                build();
    }

    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE userInfo "
                    + " ADD COLUMN phone_num TEXT");
        }
    };

    //    当添加的数据类型为 int 时，需要添加默认值 "ALTER TABLE department " + " ADD COLUMN phone_num INTEGER NOT NULL DEFAULT 0"
    public abstract UserInfoDao getUserDao();
}




















